package com.crm.service.impl.customerManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customMapper.customerManagerCustomMapper.SingleProductCustomerContactCustomMapper;
import com.crm.dao.mapper.customerManager.SingleProductCustomerContactMapper;
import com.crm.model.entity.customerManager.SingleProductCustomerContact;
import com.crm.model.entity.customerManager.SingleProductCustomerContactExample;
import com.crm.service.customerManager.SingleProductCustomerContactDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/5/12 18:33
 */
@Service
public class SingleProductCustomerContactDbServiceImpl implements SingleProductCustomerContactDbService {
    @Autowired
    private SingleProductCustomerContactMapper singleProductCustomerContactMapper;

    @Autowired
    private SingleProductCustomerContactCustomMapper singleProductCustomerContactCustomMapper;

    /**
     * 单品客户联系人
     * @Author pym
     * @Date  2021/5/12 18:37
     * @param singleProductCustomerId 单品客户主键ID
     * @param accountId 账号ID
     * @return {@link {java.util.List<com.crm.model.entity.customerManager.SingleProductCustomerContact>}}
     */
    @Override
    public List<SingleProductCustomerContact> selectSingleProductCustomerContactList(String singleProductCustomerId,
                                                                                     String accountId) {
        SingleProductCustomerContactExample singleProductCustomerContactExample = new SingleProductCustomerContactExample();
        singleProductCustomerContactExample.setOrderByClause("create_tm desc");
        SingleProductCustomerContactExample.Criteria criteria = singleProductCustomerContactExample.createCriteria();
        criteria.andSingleProductCustomerIdEqualTo(singleProductCustomerId);
        criteria.andCreatePeopleIdEqualTo(accountId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        return singleProductCustomerContactMapper.selectByExample(singleProductCustomerContactExample);
    }

    /**
     * 新增单品客户联系人
     * @Author pym
     * @Date  2021/5/13 10:43
     * @param singleProductCustomerContact 单品客户联系人
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean addSingleProductCustomerContact(SingleProductCustomerContact singleProductCustomerContact) {
        Boolean result = false;
        int num = singleProductCustomerContactMapper.insertSelective(singleProductCustomerContact);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 判断联系方式是否重复
     * @Author pym
     * @Date  2021/5/13 11:20
     * @param contactInformation 联系方式
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean isContactInformationRepeated(String contactInformation) {
        Boolean result = false;
        SingleProductCustomerContactExample singleProductCustomerContactExample = new SingleProductCustomerContactExample();
        SingleProductCustomerContactExample.Criteria criteria = singleProductCustomerContactExample.createCriteria();
        //criteria.andSingleProductCustomerIdEqualTo(singleProductCustomerId);
        criteria.andContactInformationEqualTo(contactInformation);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<SingleProductCustomerContact> singleProductCustomerContactList = singleProductCustomerContactMapper.selectByExample(singleProductCustomerContactExample);
        if (singleProductCustomerContactList.size() > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 单品客户线上跟进联系人
     * @Author pym
     * @Date  2021/5/13 14:15
     * @param singleProductCustomerId 单品客户主键ID
     * @param accountId 账号ID
     * @return {@link {java.util.List<com.crm.model.entity.customerManager.SingleProductCustomerContact>}}
     */
    @Override
    public List<SingleProductCustomerContact> selectSingleProductCustomerContactById(String singleProductCustomerId, String accountId) {
        return singleProductCustomerContactCustomMapper.selectSingleProductCustomerContactById(singleProductCustomerId,accountId);
    }

    /**
     * 单品客户-跟进-联系人回显
     * @Author pym
     * @Date  2021/5/13 15:41
     * @param singleProductCustomerId   --单品客户id
     * @param contactName  --联系人姓名
     * @return {@link {java.util.List<com.crm.model.entity.customerManager.SingleProductCustomerContact>}}
     */
    @Override
    public List<SingleProductCustomerContact> selectSingleProductCustomerContacts(String singleProductCustomerId, String contactName, String accountId) {
        return singleProductCustomerContactCustomMapper.selectSingleProductCustomerContacts(singleProductCustomerId,contactName,accountId);
    }

    /**
     * 根据单品客户id和联系方式判重，是否插入单品客户联系人
     * @Author pym
     * @Date  2021/5/13 21:17
     * @param contactInformation 联系方式
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public SingleProductCustomerContact isInsertSingleProductCustomerContact(String contactInformation) {
        SingleProductCustomerContactExample singleProductCustomerContactExample = new SingleProductCustomerContactExample();
        SingleProductCustomerContactExample.Criteria criteria = singleProductCustomerContactExample.createCriteria();
        criteria.andContactInformationEqualTo(contactInformation);
        //criteria.andSingleProductCustomerIdEqualTo(singleProductCustomerId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<SingleProductCustomerContact> singleProductCustomerContactList = singleProductCustomerContactMapper.selectByExample(singleProductCustomerContactExample);
        if (singleProductCustomerContactList.size() > 0) {
            return singleProductCustomerContactList.get(0);
        }
        return null;
    }

    /**
     * 新增单品客户联系人
     * @Author pym
     * @Date  2021/5/13 21:32
     * @param singleProductCustomerContact 单品客户联系人
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean insertSingleProductCustomerContact(SingleProductCustomerContact singleProductCustomerContact) {
        Boolean result = false;
        int num = singleProductCustomerContactMapper.insertSelective(singleProductCustomerContact);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 单品客户线上跟进判断联系人是否存在
     * @Author pym
     * @Date  2021/6/16 14:09
     * @param singleProductCustomerId 单品客户Id
     * @param contactInformation 联系方式
     * @param accountId 账号Id
     * @return {@link {com.crm.model.entity.customerManager.SingleProductCustomerContact}}
     */
    public SingleProductCustomerContact getSingleProductCustomerContactById(String singleProductCustomerId, String contactInformation, String accountId) {
        SingleProductCustomerContactExample singleProductCustomerContactExample = new SingleProductCustomerContactExample();
        SingleProductCustomerContactExample.Criteria criteria = singleProductCustomerContactExample.createCriteria();
        criteria.andSingleProductCustomerIdEqualTo(singleProductCustomerId);
        criteria.andContactInformationEqualTo(contactInformation);
        criteria.andCreatePeopleIdEqualTo(accountId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<SingleProductCustomerContact> list = singleProductCustomerContactMapper.selectByExample(singleProductCustomerContactExample);
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    /**
     * 查询单品客户下联系人Ids
     * @Author pym
     * @Date  2021/7/15 11:19
     * @param singleProductCustomerId 单品客户Id
     * @return {@link {java.util.List<java.lang.String>}}
     */
    @Override
    public List<String> selectSingleProductCustomerContactIdList(String singleProductCustomerId) {
        return singleProductCustomerContactCustomMapper.selectSingleProductCustomerContactIdList(singleProductCustomerId);
    }

    /**
     * 批量删除单品客户下联系人
     * @Author pym
     * @Date  2021/7/15 11:21
     * @param ids 单品客户下联系人Ids
     * @return {@link {int}}
     */
    @Override
    public int batchDelSingleProductCustomerContactByIds(List<String> ids) {
        return singleProductCustomerContactCustomMapper.batchDelSingleProductCustomerContactByIds(ids);
    }

}
